const base = require("../base/controllerBase");
const moment = require("moment");

class LinkageController extends base {

    async createDeviceMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;

            const model = {
                devices: {type: 'array'}
            };
            const {devices} = body;
            ctx.validate(model, body);
            const result = await ctx.app.dbs.insertMany('IotDevice', devices);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async clearDevice() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                treeId: {type: 'string'}
            };

            ctx.validate(model, body);
            //   delete body['tenantCode'];
            const {treeId} = body;
            console.log('clear device', body);
            const result = await ctx.app.dbs.deleteMany('IotDevice', {treeId});
            const result1 = await ctx.app.dbs.deleteMany('IotReportData', {treeId});
            const result2 = await ctx.app.dbs.deleteMany('DeviceCache', {treeId});

            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                treeId: {type: 'string'}
            };
            console.log('find linkage', body);
            ctx.validate(model, body);
            //   delete body['tenantCode'];
            const {treeId, deviceId, status, deviceName} = body;
            console.log('findDeviceMany bodyy', body);
            const query = {treeId};
            if (status) query.status = status;
            if (deviceId) {
                query['deviceId'] = {$regex: deviceId};
            }
            if (deviceName) {
                query['deviceName'] = {$regex: deviceName};
            }
            console.log('queryquery', query);
            const result = await ctx.app.dbs.find('IotDevice', query);
            console.log('find device many', result);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findDeviceDataMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            console.log(body);
            delete body['tenantCode'];
            const model = {
                deviceId: {type: 'string'}
            };
            const {deviceId, time} = body;
            ctx.validate(model, body);
            const query = {deviceId};
            if (time && time.length === 2) {
                const start = moment(time[0]).valueOf();
                const end = moment(time[1]).valueOf();
                query['time'] = {$gt: start, $lt: end};
            }

            const result = await ctx.app.dbs.find('IotReportData', query, {time: -1});
            //   console.log('find device data many', result);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async settingAirInitValueBatch() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            console.log(body);
            const model = {
                initValue: {type: 'number'}
            };

            ctx.validate(model, body);
            const {initValue} = body;
            const result = await ctx.app.dbs.updateMany('IotDevice', {deviceType: '末端空调'}, {initValue});
            console.log('find device many', result);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async settingAirInitValue() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            console.log(body);
            const model = {
                initValue: {type: 'number'},
                deviceId: {type: 'string'}
            };

            ctx.validate(model, body);
            const {initValue, deviceId} = body;
            const result = await ctx.app.dbs.updateOne('IotDevice', {deviceType: '末端空调', deviceId}, {initValue});
            console.log('find device many', result);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findAirInitValue() {
        const {ctx, service} = this;
        try {
            const result = await ctx.app.dbs.find('IotDevice', {deviceType: '末端空调'});
            console.log('find device many', result);
            ctx.body = {status: "ok", result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }
}

module.exports = LinkageController;
